#include "UserInterface.h"
#include <iostream>
#include <conio.h>

#include "pages/preview-menu/PreviewMenu.h"

using namespace std;


void UserInterface::run() {
    while (1) {
        switch (currentPage) {
            case Pages::PREVIEW_MENU:
                previewMenu->displayMenu();
                break;
            case Pages::MAIN_MENU:
                mainMenu->displayMenu();
                break;
            case Pages::SOLDIERS_MENU:
                soldiersMenu->displayMenu();
                break;
            case Pages::CREWS_MENU:
                crewsMenu->displayMenu();
                break;
            case Pages::BATTALIONS_MENU:
                battalionsMenu->displayMenu();
                break;
            case Pages::UNITS_MENU:
                unitsMenu->displayMenu();
                break;
            case Pages::MISSILES_MENU:
                missilesMenu->displayMenu();
                break;
            case Pages::ABOUT_AUTHOR_MENU:
                aboutAuthorMenu->displayMenu();
                break;
            case Pages::ABOUT_PROGRAM_MENU:
                aboutProgramMenu->displayMenu();
                break;
            case Pages::OPERATOR_MANUAL_MENU:
                operatorManualMenu->displayMenu();
                break;
            case Pages::EXIT:
                return;
        }
    }
}

Keys::Key UserInterface::getPressedKey() {
    char pressedKey = _getch();
    return Keys::charToKey(pressedKey);
}

void UserInterface::toggleLocale() {
    string curLocale = t.getCurrentLocale();
    if (curLocale == "ru") {
        t.setLocale("en");
    } else if (curLocale == "en") {
        t.setLocale("ru");
    }
}

Pages UserInterface::getCurrentPage() const {
    return currentPage;
}

void UserInterface::setCurrentPage(Pages currentPage) {
    this->currentPage = currentPage;
}

UserInterface::UserInterface(
        std::string defaultLocale,
        std::vector<std::string> supportedLocales,
        MainService *mainService
) : mainService(mainService) {

    config.supportedLocales = supportedLocales;
    t = i18n::Translator(config);
    t.setLocale(defaultLocale);

    mainMenu = new MainMenu(&t, this, this->mainService);
    previewMenu = new PreviewMenu(&t, this);
    soldiersMenu = new SoldiersMenu(&t, this, mainService->soldiersService);
    crewsMenu = new CrewsMenu(&t, this, mainService->crewsService);
    unitsMenu = new UnitsMenu(&t, this, mainService->unitsService);
    battalionsMenu = new BattalionsMenu(&t, this, mainService->battalionsService);
    missilesMenu = new MissilesMenu(&t, this, mainService->missilesService);
    aboutProgramMenu = new AboutProgramMenu(&t, this);
    aboutAuthorMenu = new AboutAuthorMenu(&t, this);
    operatorManualMenu = new OperatorManualMenu(&t, this);
    currentPage = Pages::PREVIEW_MENU;
}

UserInterface::~UserInterface() {
    delete mainMenu;
    delete previewMenu;
    delete soldiersMenu;
    delete crewsMenu;
    delete unitsMenu;
    delete battalionsMenu;
    delete missilesMenu;
    delete aboutProgramMenu;
    delete aboutAuthorMenu;
    delete operatorManualMenu;
}
